package controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.JDBCUtil;


@WebServlet("/studentDelete")
public class StudentDeleteServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// /Java201202/studentDelete?id=9
		String id = req.getParameter("id");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `student` WHERE `id`=?";
			preparedStatement = connection.prepareStatement(sql);
			// parameter:参数
			preparedStatement.setInt(1, Integer.parseInt(id));
			int count = preparedStatement.executeUpdate();
			System.out.println(preparedStatement);
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		// 删除完了之后，再去查找所有学生,展示最新的数据
		// resp.sendRedirect("/Java201202/student");
		resp.sendRedirect(req.getContextPath() + "/student");
	}
}
